package cn.itcast.ks.service.impl;

import cn.itcast.ks.common.ResponseResult;
import cn.itcast.ks.common.threadlocal.UserThreadLocalUtils;
import cn.itcast.ks.mapper.UserStudentAssignmentMapper;
import cn.itcast.ks.pojo.Vos.AssignmentVo;
import cn.itcast.ks.pojo.dtos.NotWorkDto;
import cn.itcast.ks.pojo.dtos.ShowWorkDto;
import cn.itcast.ks.pojo.dtos.WorkDto;
import cn.itcast.ks.pojo.entity.User;
import cn.itcast.ks.pojo.entity.UserStudentAssignment;
import cn.itcast.ks.service.IUserService;
import cn.itcast.ks.service.UserStudentAssignmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author mwj
 * @date 2021/12/12
 * @TIME:15:18
 */
@Service
public class UserStudentAssignmentServiceImpl extends ServiceImpl<UserStudentAssignmentMapper, UserStudentAssignment> implements UserStudentAssignmentService {
    @Autowired
    private IUserService iUserService;
    @Autowired
    private UserStudentAssignmentService userStudentService;
    @Autowired
    private UserStudentAssignmentMapper userStudentAssignmentMapper;

    //展示学生作业列表
    @Override
    public ResponseResult showWorks(ShowWorkDto showWorkDto) {
        // 只能显示当前班级的同学信息，如何判断--- 子查询
        //获取班级id
        User teacher = UserThreadLocalUtils.getUser();
        Integer classId = teacher.getClassId();
        //获取日期和type
        Date time = showWorkDto.getTime();
        int type = showWorkDto.getType();

        List<UserStudentAssignment> userStudentAssignments = userStudentAssignmentMapper.showWorks(time, type, classId);

        //获取userstudentassignment
//        LambdaQueryWrapper<UserStudentAssignment> wrapper=new LambdaQueryWrapper<>();
        //根据时间和类型
//        wrapper.eq(UserStudentAssignment::getCreatedOn,showWorkDto.getTime()).eq(UserStudentAssignment::getAssignmentType,showWorkDto.getType());
//        List<UserStudentAssignment> userStudentAssignments = userStudentService.list(wrapper);
        // System.out.println("userStudentAssignments+"+userStudentAssignments);


        //先获得userid。可能存在一个人做多份作业情况，但id唯一
        HashSet<Integer> uids = new HashSet<>();
        for (UserStudentAssignment userStudentAssignment : userStudentAssignments) {
            Integer userId = userStudentAssignment.getUserId();
            uids.add(userId);
        }
        //System.out.println("uids"+uids);
        //根据uids遍历，获得他当天所做作业list，封装到practicelist
        //{{},{},{}}
        List<AssignmentVo> assignmentVos = new ArrayList<>();
        //Work work=new Work();
        for (Integer uid : uids) {
            AssignmentVo assignmentVo = new AssignmentVo();
            //通过uid找到名字
            User user = iUserService.getById(uid);
            String name = user.getName();
            //将uid，名字先存入vo
            assignmentVo.setUid(uid);
            assignmentVo.setName(name);
            //通过id比较，找出同uid的所有作业
            //将作业封装到map，再放入list中
            List<Map> practiceList = new ArrayList<>();
            for (UserStudentAssignment userStudentAssignment : userStudentAssignments) {
                if (uid.equals(userStudentAssignment.getUserId())) {
                    Map map = new HashMap();
                    map.put("id", userStudentAssignment.getAssignmentId());
                    map.put("workname", userStudentAssignment.getAssignmentName());
                    map.put("isComplete", userStudentAssignment.getStatus());
                    practiceList.add(map);
                }
            }
            assignmentVo.setPracticeList(practiceList);
            //System.out.println("assignmentVo="+assignmentVo);
            assignmentVos.add(assignmentVo);
        }
        //System.out.println("assignmentVos="+assignmentVos);
        return ResponseResult.okResult(assignmentVos);


    }

    //查询时间段未提交作业的学生以及次数
    @Override
    public ResponseResult findNotWorkByTime(NotWorkDto notWorkDto) {

        List<Map> maps = userStudentAssignmentMapper.findNotWorkByTime(notWorkDto);
        for (Map map : maps) {
            Integer userId = (Integer) map.get("userId");
            Long count = (Long) map.get("count(*)");
            map.put("count", count);
            User user = iUserService.getById(userId);
            map.put("name", user.getName());
            //System.out.println(map);
        }
        return ResponseResult.okResult(maps);
    }

    //查询作业列表（学生接口）
    @Override
    public ResponseResult findStudentWork(ShowWorkDto showWorkDto) {
        //先明确登录的是学生id，应该只能看到自己的作业
        User user = UserThreadLocalUtils.getUser();
        Integer userId = user.getId();
        String name = user.getName();
        //先获得该学生在固定时间和类型下的所有作业信息
        Date time = showWorkDto.getTime();
        int type = showWorkDto.getType();
        List<UserStudentAssignment> assignments = userStudentAssignmentMapper.findStudentWork(userId, time, type);
        List<AssignmentVo> assignmentVos = new ArrayList<>();
        //
        List<Map> practiceList = new ArrayList<>();
        for (UserStudentAssignment assignment : assignments) {
            Map map = new HashMap();
            map.put("workname", assignment.getAssignmentName());
            map.put("id", assignment.getAssignmentId());
            map.put("isComplete", assignment.getStatus());
            practiceList.add(map);
        }
        AssignmentVo assignmentVo = new AssignmentVo();
        assignmentVo.setUid(userId);
        assignmentVo.setName(name);
        assignmentVo.setPracticeList(practiceList);
        //为了跟返回值匹配
        assignmentVos.add(assignmentVo);
        return ResponseResult.okResult(assignmentVos);
    }

    //学生提交作业（学生接口）
    @Override
    public ResponseResult submitWork(Integer workId, Integer status) {
        //先获得学生id
        User user = UserThreadLocalUtils.getUser();
        Integer userId = user.getId();
        //根据学生id和作业id,将status改变
        userStudentAssignmentMapper.submitWork(workId,userId,status);
        if(status==2){
            return ResponseResult.okResult("作业提交成功");
        }else{
            return ResponseResult.okResult("作业取消成功");
        }
    }


}
